import store from '@/store'
import { generalDateFormat } from '@/utils/common'
function splitListStr (str, splitstr, index) {
  let startIndex = str.indexOf(splitstr, index)
  if (startIndex === -1) return []
  let endIndex = str.indexOf(splitstr.replace('<', '</'), startIndex + 1)
  return [[startIndex, endIndex], ...splitListStr(str, splitstr, endIndex)]
}
function listToStr (str, type) {
  for (let i = 1; i < 9; i++) {
    if (!str.includes(`ql-indent-${i}`)) continue
    let regStr = new RegExp(`<li class="ql-indent-${i}"(.*?)>`, 'g')
    Array.isArray(str.match(regStr)) &&
      str.match(regStr).forEach(v => {
        str.replaceAll(v, v.substr(0, 23) + '>')
      })
  }
  let startIndex = str.indexOf(type)
  let endIndex = str.lastIndexOf(type.replace('<', '</'))
  let endStr = str.substring(startIndex + 4, endIndex)
  endStr.replaceAll(type, '')
  endStr.replaceAll(type.replace('<', '</'), '')
  return str.substring(0, startIndex + 4) + endStr + str.substr(endIndex)
}
function strToList (str, type) {
  for (let i = 1; i < 9; i++) {
    if (!str.includes(`ql-indent-${i}`)) continue
    if (
      i > 1 &&
      !(str.includes(`ql-indent-${i}`) && str.includes(`ql-indent-${i - 1}`))
    ) {
      continue
    }
    let regStr =
      i === 1
        ? new RegExp(`<li class="ql-indent-${i}">(.*?)</li><li>`, 'g')
        : new RegExp(
          `<li class="ql-indent-${i}">(.*?)</li><li class="ql-indent-${i -
              1}">`,
          'g'
        )
    Array.isArray(str.match(regStr)) &&
      str.match(regStr).forEach(v => {
        let replaceStr =
          i === 1 ? v.substring(0, v.length - 4) : v.substring(0, v.length - 24)
        str = str.replace(
          replaceStr,
          `${type}${replaceStr}${type.replace('<', '</')}`
        )
      })
  }
  if (type === '<ol>') {
    let listTypeArr = ['i', 'a']
    for (let i = 1; i < 9; i++) {
      str = str.replaceAll(
        `<li class="ql-indent-${i}">`,
        `<li class="ql-indent-${i}" type="${listTypeArr[i % 2]}">`
      )
    }
  }
  return str
}
export function replaceEmailContent (str) {
  if (!str || typeof str !== 'string') return str
  str = str.replace(/<strong>{{(.*?)}}<\/strong>/g, (_, item)=>`<strong>{{${item.replace(/&nbsp;/g, ' ')}}}</strong>`);
  if (str.includes('type=') && str.includes('<ol>')) {
    str = listToStr(str, '<ol>')
  }
  if (str.includes('type=') && str.includes('<ul>')) {
    str = listToStr(str, '<ul>')
  }
  let olArr = splitListStr(str, '<ol>', 0)
  olArr.forEach(([startIndex, endIndex]) => {
    str =
      str.substring(0, startIndex) +
      strToList(str.substring(startIndex, endIndex), '<ol>') +
      str.substring(endIndex, str.length)
  })
  let ulArr = splitListStr(str, '<ul>', 0)
  ulArr.forEach(([startIndex, endIndex]) => {
    str =
      str.substring(0, startIndex) +
      strToList(str.substring(startIndex, endIndex), '<ul>') +
      str.substring(endIndex, str.length)
  })
  return str
}
export function replaceUserDateFormat (str) {
  if (!str || typeof str !== 'string') {
    return str
  }
  return str.replaceAll('{CurrentDate}', generalDateFormat(+new Date(), null, store.state.user.timeFormat))
}
